package eg.com.itiga.ui.actions;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import eg.com.itiga.business.base.BaseService;
import eg.com.itiga.business.factory.BusinessFactory;
import eg.com.itiga.business.role.RoleService;
import eg.com.itiga.domain.Branch;
import eg.com.itiga.domain.Department;
import eg.com.itiga.domain.Itian;
import eg.com.itiga.domain.Role;
import eg.com.itiga.domain.User;
import eg.com.itiga.ui.beans.AddITIianFormBean;

public class AddITIianAction extends org.apache.struts.action.Action {
	
	private Itian itian;
    
    public AddITIianAction() {
    }
    
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {		
        System.out.println("I'm action!");
    	ActionForward forward=new ActionForward();
        AddITIianFormBean formBean=(AddITIianFormBean) form;
        try{
        	
        Integer branchID=formBean.getBranch();
        Integer depID=formBean.getDepartment();
        String name=formBean.getFullName();
        String email=formBean.getEmail();
        String phone=formBean.getPhone();
        Integer intake=formBean.getIntake();
        
        BaseService baseService= BusinessFactory.createBaseService();
		Branch branch=(Branch) baseService.find(Branch.class, branchID);
		Department department=(Department) baseService.find(Department.class, depID);
		
		
		User user=new User();
		user.setUserName("username1");
		user.setUserPassword("password1");
		
		RoleService roleService=BusinessFactory.createRoleService();
		Role r=roleService.getRoleByName("ITIan");
		user.setRole(r);
		
		Itian i=new Itian(department,user,branch,name,email,phone,intake,null,null,null,null,null);
		baseService.add(i);
		
        }catch (Exception e) {
			e.printStackTrace();
		}
        forward=mapping.findForward("success");
    	return forward;
    }

}